package com.mmdccj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mmdccj.reggie.common.CustomException;
import com.mmdccj.reggie.entity.Category;
import com.mmdccj.reggie.entity.Dish;
import com.mmdccj.reggie.entity.Setmeal;
import com.mmdccj.reggie.mapper.CategoryMapper;
import com.mmdccj.reggie.service.CategoryService;
import com.mmdccj.reggie.service.DishService;
import com.mmdccj.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /***
     * 删除套餐菜品的完善
     *
     * @param id
     */
    @Override
    public void remove(long id) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishQueryWrapper);
        if (count > 0) {
            throw new CustomException("当前类型有关联菜品,无法删除");
        }
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealQueryWrapper);
        if (count2 > 0){
            throw new CustomException("当前类型有关联套餐,无法删除");
        }
        super.removeById(id);
    }
}



